<?php
/**
 * Demo Admin
 * @author wangji <j@wj45.net>
 */
 
class indexAction
{
	private $_tpl;
    private $_logo='http://img01.taobaocdn.com/imgextra/i1/135326559/T2bfkbXnFaXXXXXXXX_!!135326559.png';
	private $_user;	
	private $_menu=array(
		array('link'=>'admin.php?r=index/index', 'desc'=>'首页'),
		array('link'=>'admin.php?r=index/doGood', 'desc'=>'商品管理'),
		array('link'=>'admin.php?r=index/doAd', 'desc'=>'广告管理'),
		array('link'=>'admin.php?r=index/doCa', 'desc'=>'分类管理'),
		array('link'=>'admin.php?r=index/doUser', 'desc'=>'管理员账号管理'),
	);
    public $route;

    public function __construct() {
        $this->_tpl = new TPL();
        $rt=Base::getRoute();
        $this->route='r=' . $rt['c'] . '/' . $rt['a'];
    }

    public function index() {
		$this->check_login();
		$this->_user=$_SESSION["user"];

        $css_info=array('pages/js/themes/default/easyui.css',
                        'pages/js/themes/icon.css',
						'pages/js/themes/admin.css',
        );
		$this->_tpl->render(
			'index.html', array('user'=>$this->_user,
					'title'=>'海涛网络超市-管理平台',
					'css_info'=>$css_info,
					'menu'=>$this->_menu,
					'current'=>'首页',
					'main'=>'',
					'js_info'=>array(),
				)
		);		
    }
	public function doGood(){
		$this->check_login(); 
		$this->_user=$_SESSION["user"];

		$js_info=array('//lib.sinaapp.com/js/jquery/2.0/jquery.min.js',
					   'pages/js/jquery.easyui.min.js',
					   'pages/js/details.js',
					   'pages/js/product.js',
				);
		$css_info=array('pages/js/themes/default/easyui.css',
						'pages/js/themes/icon.css',
						'pages/js/themes/admin.css',
				);

		$ca=new caModel();
        $ca_leaf=$ca->getCaLeaf();
		$content=$this->_tpl->getRender('product.html', array(
				'ca'=>$ca_leaf	
			)
		);
        $this->_tpl->render(
            'index.html', array('user'=>$this->_user,
                    'title'=>'海涛网络超市-管理平台',
                    'css_info'=>$css_info,
                    'menu'=>$this->_menu,
                    'current'=>'宝贝管理',
                    'main'=>$content,
                    'js_info'=>$js_info,
				)
		);		
	}
	public function getProduct() {
		$this->check_login();
		$product=new productModel();
		
		$page=intval(Base::getRequest('page'));
		$rows=intval(Base::getRequest('rows'));

		$condition='';
		$name=Base::getRequest('name');
		$status=Base::getRequest('status');
		$category=Base::getRequest('category');
		$and='';
		if($status != -1 && $status<>false) {
			$condition .= 'status=' . $status . ' ';
			$and='and';
		}
		if($category != -1 && $category<>false ) {
			$condition .= ' ' . $and . ' category=' . $category . ' ';
			$and='and';
		}
		if($name != false) $condition .= ' ' . $and . ' name LIKE \'%' . $name .'%\' ';
		
		$sort_str='';
		$sort=Base::getRequest('sort');
		$order=Base::getRequest('order');
		if(!empty($sort)) $sort_str .= 'order by ' . $sort . ' ';
		if(!empty($order)) $sort_str .= $order;
		
		$result=$product->getProduct($page, $rows, $condition, $sort_str);
		echo json_encode($result);
	}
	public function showGoodForm(){
		$this->check_login();

		$ca=new caModel();
		$ca_leaf=$ca->getCaLeaf();
		$index=intval(Base::getRequest('index'));
		$this->_tpl->render('good_form.html', array('index'=>$index, 'ca'=>$ca_leaf));
	}	
	public function saveProduct() {
		$this->check_login();
		
		$product=new productModel();
		$result=$product->saveProduct();
		echo $result;
	}
	public function destoryProduct() {
		$this->check_login();

		$id=Base::getRequest('id');
		if($id === false)
			echo json_encode(array('success'=>false));
		$product=new productModel();
		echo $result=$product->destoryProduct($id);	
	}
	public function updateProduct(){
		$this->check_login();

		$product=new productModel();
        $result=$product->updateProduct();
        echo $result;	
	}

	public function doAd() {
		$this->check_login(); 

		
    }
	public function doCa() {
		$this->check_login(); 

    }
	public function doUser() {
		$this->check_login(); 

        $this->_user=$_SESSION["user"];

		$js_info=array('//lib.sinaapp.com/js/jquery/2.0/jquery.min.js',
					   'pages/js/jquery.easyui.min.js',
                       'pages/js/md5.js'
				);
		$css_info=array('pages/js/themes/default/easyui.css',
						'pages/js/themes/icon.css',
						'pages/js/themes/admin.css',
				);

		$content=$this->_tpl->getRender('auser.html', array(
			)
		);
        $this->_tpl->render(
            'index.html', array('user'=>$this->_user,
                    'title'=>'海涛网络超市-管理平台',
                    'css_info'=>$css_info,
                    'menu'=>$this->_menu,
                    'current'=>'宝贝管理',
                    'main'=>$content,
                    'js_info'=>$js_info,
				)
		);
	}
    public function getAuser(){
        $this->check_login();
        
        $auser=new auserModel();
        echo $auser->getAuser();
    }
    public function saveAuser(){
        $this->check_login();
        
        $auser=new auserModel();
        echo $auser->saveAuser();
    }
    public function updateAuser(){
        $this->check_login();
        
        $auser=new auserModel();
        echo $auser->updateAuser();
    }
    public function destroyAuser(){
        $this->check_login();
        
        $auser=new auserModel();
        echo $auser->destroyAuser();
    }
    
    
	public function login(){
		if($this->is_login()) 
			header('Location:' . APP_URL . 'admin.php');
		
		$user=Base::getRequest('username');
		$password=Base::getRequest('password');
				
		$varify=true;
		if(Base::getRequest('submit') && (empty($user) || preg_replace("^[a-zA-Z0-9_]", " ", $user)==$user) ){
			$info='用户名或密码错误';
			$varify=false;
		}
		if(Base::getRequest('submit') & (empty($password) || preg_replace("^[a-zA-Z0-9_]", " ", $password)==$password ) ){
			$info='用户名或密码错误';
			$varify=false;
		}
			
		if($varify==true && Base::getRequest('submit')){
			$User=new auserModel();
			if($User->verify($user, $password)){
				$_SESSION["user"]=$user;
				header('Location:' . APP_URL . 'admin.php');
			}
			else
				header('Location:' . APP_URL . 'admin.php?r=index/login&info=' . '用户名或者密码错误');
		}
		$info .= Base::getRequest('info');
		if(empty($info)) $info='请您登录';
		$this->_tpl->render('login.html', array(
				'info'=>$info,
			)
		);
	}

	public function logout(){
		session_unset();
		session_destroy();
		header('Location:' . APP_URL . 'admin.php?r=index/login');
	}




	private function is_login(){
		if(!isset($_SESSION["user"]) || empty($_SESSION["user"])) 
			return false;
		else
			return true;
	}
	private function check_login(){
		if(!$this->is_login())
            header('Location:' . APP_URL . 'admin.php?r=index/login&info=' . '需要登录后才能够访问');
	}
}

/* ------ EOF ------ */
